package com.xinke.edu.mypetadoption.Activity.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.xinke.edu.mypetadoption.R;
import com.xinke.edu.mypetadoption.entity.Orders;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.OrderViewHolder> {
    private List<Orders> orderList;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public OrderAdapter() {
    }

    // 设置订单数据
    public void setOrderList(List<Orders> orderList) {
        this.orderList = orderList;
        notifyDataSetChanged();
    }

    @Override
    public OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order, parent, false);
        return new OrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(OrderViewHolder holder, int position) {
        if (orderList != null && position < orderList.size()) {
            Orders order = orderList.get(position);
            
            // 绑定订单数据
            holder.orderName.setText(order.getProductName());
            holder.orderId.setText(order.getOrderNo());
            holder.orderStatus.setText(getOrderStatusText(order.getStatus()));
            holder.orderTime.setText(order.getCreatedAt());
            holder.totalPrice.setText("￥" + order.getTotalAmount());
            
            // 这里可以根据需要绑定更多的订单详情信息
            // 例如商品列表、数量等
        }
    }

    @Override
    public int getItemCount() {
        return orderList == null ? 0 : orderList.size();
    }

    // 获取订单状态文本
    private String getOrderStatusText(int status) {
        switch (status) {
            case 0:
                return "待付款";
            case 1:
                return "待发货";
            case 2:
                return "待收货";
            case 3:
                return "已完成";
            default:
                return "未知状态";
        }
    }

    // 格式化日期
    private String formatDate(Date date) {
        if (date != null) {
            return dateFormat.format(date);
        }
        return "";
    }

    static class OrderViewHolder extends RecyclerView.ViewHolder {
        TextView orderId;
        TextView orderStatus;
        TextView orderTime;
        TextView totalPrice;
        TextView orderName;

        public OrderViewHolder(View itemView) {
            super(itemView);
            orderName = itemView.findViewById(R.id.tv_product_name);
            orderId = itemView.findViewById(R.id.tv_order_no);
            orderStatus = itemView.findViewById(R.id.tv_order_status);
            orderTime = itemView.findViewById(R.id.tv_order_time);
            totalPrice = itemView.findViewById(R.id.tv_total_price);
        }
    }
}